/*
 * �쐬��: 2008/07/31
 *
 */
package sort;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sort.record.Record;
import sort.record.Records;

/**
 * @author masaru-gotou
 *
 */
@Deprecated
@SuppressWarnings("serial")
public abstract class SortingViewer extends JFrame implements Observer,ActionListener {
	private JButton b_start;
	private JButton b_stop;
	private JButton b_init;
	private JLabel l_sort_algo;
	private JLabel l_time;
	private JPanel v_panel;
	private Image d_buffer = null;
	private boolean inited = false;
	private JLabel l_validated;
	private JLabel l_sorted;
	private JLabel l_mem_usage;
	private JLabel l_stabled;

	/**
	 * @param gc
	 */
	public SortingViewer(GraphicsConfiguration gc) {
		super(gc);
		init_inner();
	}

	/**
	 * @param title
	 * @param gc
	 */
	public SortingViewer(String title, GraphicsConfiguration gc) {
		super(title, gc);
		init_inner();
	}

	/**
	 * @param title
	 * @throws HeadlessException
	 */
	public SortingViewer(String title) throws HeadlessException {
		super(title);
		init_inner();
	}

	/**
	 * 
	 *
	 */
	public SortingViewer() {
		super();
		init_inner();
	}
	
	private void init_inner() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 400);
		createComponents();
	}

	private void createComponents() {
		setLookAndFeel();
		
		Container container = getContentPane();
		container.setLayout(new GridLayout(2,1));
		
		v_panel = new JPanel(new GridLayout(1,1));
		v_panel.setBounds(0, 0, getWidth(), 300);
		container.add(v_panel);
		JPanel cpanel = new JPanel(new FlowLayout());
		container.add(cpanel);
		cpanel.setBounds(0, 0, getWidth(), 50);
		
//		list = new JList();
//		v_panel.add(new JScrollPane(list));
		
		
		b_start = new JButton("START");
		b_start.setEnabled(inited);
		cpanel.add(b_start);
		b_stop = new JButton("STOP");
		cpanel.add(b_stop);
		b_init = new JButton("INIT");
		cpanel.add(b_init);
		
		l_sort_algo = new JLabel();
		cpanel.add(l_sort_algo);
		l_time = new JLabel();
		cpanel.add(l_time);
		l_validated = new JLabel();
		cpanel.add(l_validated);
		l_sorted = new JLabel();
		cpanel.add(l_sorted);
		l_stabled = new JLabel();
		cpanel.add(l_stabled);
		l_mem_usage = new JLabel();
		cpanel.add(l_mem_usage);
		
		b_init.addActionListener(this);
		b_start.addActionListener(this);
		b_stop.addActionListener(this);
	}

	private void setLookAndFeel() {
//		try {
//			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//			SwingUtilities.updateComponentTreeUI(this);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}

	public void update(Observable o, Object arg) {
//		Log log = LogFactory.getLog(getClass());
//		writeLog(log,(Records)arg);
		setRecords((Records)arg);
//		try {
//			Thread.sleep(100);
//		} catch (InterruptedException e) {
//			// TODO �����������ꂽ catch �u���b�N
//			e.printStackTrace();
//		}
	}

//	protected void writeLog(Log log, Records records) {
////		log.debug("recs="+records.size());
////		for (Record record : records) {
////			log.debug("rec"+record);
////		}
//		log.debug(records);
//	}

	/* (Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if ( e.getSource() == b_start ) start();
		if ( e.getSource() == b_stop ) stop();
		if ( e.getSource() == b_init ) {
			init();
			inited = true;
			b_start.setEnabled(inited);
		}
	}
	
	public void setRecords(Records records) {
//		DefaultListModel model = new DefaultListModel();
//		for (Record record : records) {
//			model.addElement(record.toString());
//		}
//		list.setModel(model);
		if ( d_buffer == null ) {
			d_buffer = v_panel.createImage(v_panel.getWidth(),v_panel.getHeight());
		}
		Graphics bufg = d_buffer.getGraphics();
		bufg.clearRect(0, 0, v_panel.getWidth(), v_panel.getHeight());
		drawRecords(records,bufg);
		// �E�B���h�E���X�V����
		v_panel.getGraphics().drawImage(d_buffer,0,0,this);
	}
	private void drawRecords(Records records, Graphics g) {
		int lin_w = 2;
		for (int x=0;x<records.size();x++) {
			Record r = records.get(x);
			g.setColor(Color.CYAN);
			g.fillRect(x*lin_w, 0, lin_w, r.getKey());
			g.setColor(Color.BLUE);
			g.fillRect(x*lin_w,r.getKey(),lin_w,1);
		}
	}

	public void setSortAlgoName(String name ) {
		l_sort_algo.setText("algorythm="+name);
	}
	public void setTime(long time) {
		if ( time > 0 ) {
			l_time.setText("elapsed time="+time+"msec");
		}
	}
	public void setValidated(boolean validated) {
		l_validated.setText("validated="+validated);
	}
	public void setSorted(boolean sorted) {
		l_sorted.setText("sorted="+sorted);
	}
	public void setMemoryUsage(long used) {
		l_mem_usage.setText("used mem="+used+"B");
	}
	public void setStabled(boolean stabled) {
		l_stabled.setText("stabled="+stabled);
	}

	protected abstract void stop();
	protected abstract void start();
	protected abstract void init();
}
